const urlApi = require('../../utils/server.api.js')
var app = getApp();
Page({
  data: {
    isChecked: false,
    date: "",
    select_all: false,//是否全选
    sickName: "",
    sickCard: "",
    deptName: ['内科', '外科', '儿科', '妇科', '眼科', '耳鼻喉科', '口腔科', '皮肤科', '其他'],
    deptIndex: -1,
    deptNameId: [
      {deptId: 101, deptName: "内科"},
      {deptId: 102, deptName: "外科"},
      {deptId: 103, deptName: "儿科"},
      {deptId: 104, deptName: "妇科"},
      {deptId: 105, deptName: "眼科"},
      {deptId: 106, deptName: "耳鼻喉科"},
      {deptId: 107, deptName: "口腔科"},
      {deptId: 108, deptName: "皮肤科"},
      {deptId: 109, deptName: "其他"},
    ],
    regItem: ['门诊', '急诊'],
    regItemIndex: -1,
    regItemId: [
      {regItemId: 1, regItemName: "门诊", regItemFee: 6},
      {regItemId: 2, regItemName: "急诊", regItemFee: 12},
    ],
    MZLabel: ['上午', '下午', '晚上'],
    MZLabelIndex: -1,
    MZLabelId: [
      {dictLabel: "上午", dictValue: "1"},
      {dictLabel: "下午", dictValue: "2"},
      {dictLabel: "晚上", dictValue: "3"},
    ],
    list: [{}],
    registrationDto: {
      deptId: "",
      schedulingType: "",  //挂号类型
      subsectionType: "",  //挂号时段
      schedulingDay: ""  //查询时间
    },
    registrationFormDto: {
      patientDto: {},
      registrationDto: {
        deptId: "",
        regItemId: "",
        regItemAmount: "",
        schedulingType: "",
        visitDate: "",
        subsectionType: "",
      }
    },
    choseNames: 0, //选中的名字列表
    verifyCode: 0,
  },

  //全选与反全选
  selectall: function (e) {
    var arr = []; //存放选中id的数组
    for (let i = 0; i < this.data.list.length; i++) {

      this.data.list[i].checked = (!this.data.select_all)

      if (this.data.list[i].checked == true) {
        // 全选获取选中的值
        arr = arr.concat(this.data.list[i].deptName.split(','));
      }
    }
    this.setData({
      list: this.data.list,
      select_all: (!this.data.select_all),
      choseNames: arr
    })
  },

  // 单选
  checkboxChange: function (e) {
    console.log(e.detail.value)
    this.setData({
      choseNames: e.detail.value, //单个选中的值
    })
    if (this.data.choseNames.length == this.data.list.length) {
      this.setData({
        select_all: true
      })
    } else {
      this.setData({
        select_all: false
      })
    }
  },

  listDeptForScheduling:function(e){
    var that = this;
    wx.request({
      url: urlApi.getTableList(),
      method: "get",
      header: {
        'content-type': 'application/json'
      },
      success: function (response) {
        that.setData({
          list: response.data.data
        })
      }
    })
  },

  bindDateChange(options) {
    this.data.registrationDto.schedulingDay = options.detail.value
    this.setData({
      date: options.detail.value
    })
  },

  bindDeptChange: function(e) {
    this.data.registrationDto.deptId = this.data.deptNameId[e.detail.value].deptId
    this.setData({
      deptIndex: e.detail.value
    })
  },

  bindregItemChange: function(e) {
    this.data.registrationDto.schedulingType = this.data.regItemId[e.detail.value].regItemId
    this.setData({
      regItemIndex: e.detail.value
    })
  },

  bindMZLabelChange: function(e) {
    this.data.registrationDto.subsectionType = this.data.MZLabelId[e.detail.value].dictValue
    this.setData({
      MZLabelIndex: e.detail.value
    })
  },

  radioChange: function() {
    this.setData({
      isChecked: !this.data.isChecked
    })
    console.log(this.data.isChecked)
  },

  // 查询
  create_chaxun:function(e){
    var that = this;
    wx.request({
      url: urlApi.getTableList(),
      data: that.data.registrationDto,
      method: "get",
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        if (res.data.code == 200) {
          wx.showToast({
            title: "查询成功",
            duration: 2000
          })
          that.setData({
            list: res.data.data
          })
        } else {
          wx.showToast({
            title: "查询出错，请稍后再试",
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },

  goto_guahao:function(e){
    var that = this;
    if (app.globalData.userInfo == null) {
      wx.showToast({
        title: "请重新登录",
        icon: 'none',
        duration: 2000
      })
      wx.navigateTo({
        url: '../index/index',
      })
    }
    if (app.globalData.userInfo.isFinal == 0 || app.globalData.userInfo.isFinal == "0") {
      wx.showToast({
        title: "请完善个人信息",
        icon: 'none',
        duration: 2000
      })
      wx.navigateTo({
        url: '../improveInfo/improveInfo',
      })
    }
    that.data.registrationFormDto.patientDto = app.globalData.userInfo
    if (that.data.choseNames.length != 1) {
      that.data.verifyCode++
    }
    that.data.registrationFormDto.registrationDto.deptId = +that.data.choseNames
    if (that.data.registrationDto.subsectionType == "") {
      that.data.verifyCode++
    }
    that.data.registrationFormDto.registrationDto.visitDate = that.data.date
    if (that.data.registrationDto.subsectionType == "") {
      that.data.verifyCode++
    }
    that.data.registrationFormDto.registrationDto.subsectionType = that.data.registrationDto.subsectionType
    if (that.data.registrationDto.schedulingType == "") {
      that.data.verifyCode++
    }
    that.data.registrationFormDto.registrationDto.schedulingType = that.data.registrationDto.schedulingType
    if(that.data.isChecked) {
      if (that.data.registrationDto.schedulingType == "1") {
        that.data.registrationFormDto.registrationDto.regItemId = "2",
        that.data.registrationFormDto.registrationDto.regItemAmount = "7"
      } else if (that.data.registrationDto.schedulingType == "2") {
        that.data.registrationFormDto.registrationDto.regItemId = "4",
        that.data.registrationFormDto.registrationDto.regItemAmount = "13"
      }
    } else {
      if (that.data.registrationDto.schedulingType == "1") {
        that.data.registrationFormDto.registrationDto.regItemId = "1",
        that.data.registrationFormDto.registrationDto.regItemAmount = "6"
      } else if (that.data.registrationDto.schedulingType == "2") {
        that.data.registrationFormDto.registrationDto.regItemId = "3",
        that.data.registrationFormDto.registrationDto.regItemAmount = "12"
      }
    }
    if(that.data.verifyCode == 0) {
      wx.request({
        url: urlApi.addRegistration(),
        data: that.data.registrationFormDto,
        method: "post",
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          if (res.data.code == 200) {
            wx.showModal({
              title: '挂号成功',
              content: '暂不支持小程序支付，请到医院前台付费，逾期挂号作废',
              complete: (res) => {
                if (res.cancel) {
                  wx.navigateTo({
                    url: '../home/home',
                  })
                }
                if (res.confirm) {
                  wx.navigateTo({
                    url: '../home/home',
                  })
                }
              }
            })
          } else {
            wx.showToast({
              title: "挂号出错，请稍后再试",
              icon: 'none',
              duration: 2000
            })
          }
        }
      })
    } else {
      wx.showToast({
        title: "一次只能选择一个科室挂号或就诊时间、日期必填信息没填",
        icon: 'none',
        duration: 2000
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.listDeptForScheduling();
    this.setData({
      sickName: app.globalData.userInfo.name,
      sickCard: app.globalData.userInfo.idCard
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
